package handler

import (
	"github.com/martini-contrib/encoder"
	"hotel_Manager_Api/model/dbEngin"
	"hotel_Manager_Api/model/dbModel"
	"github.com/go-martini/martini"
	"strconv"
	"fmt"
)

func GetAllUnit(enc encoder.Encoder) ([]byte, error) {
	db := dbEngin.GetEnging()
	units := make([]dbModel.Unit, 0)
	err := db.OrderBy("id").Find(&units)
	if (err != nil) {
		return nil, err;
	}
	return encoder.Must(enc.Encode(units)), nil;
}

func NewOneUnit(unit dbModel.Unit) (int) {
	db := dbEngin.GetEnging()
	_,err := db.Insert(unit)
	fmt.Print(err)
	return 201
}

func EditOneUnit(params martini.Params, unit dbModel.Unit) (int) {
	db := dbEngin.GetEnging();
	i, _ := strconv.ParseInt(params["id"], 10, 16);
	id := int16(i);
	unit.Id = id;
	_,err :=db.Id(id).Update(unit)
	if(err!=nil){
		return 400
	}
	return 200
}
